package com.tuocent.dagv2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuocent.dagv2.common.AjaxResult;
import com.tuocent.dagv2.common.entity.LoginUserInfo;
import com.tuocent.dagv2.common.utils.redis.RedisCache;
import com.tuocent.dagv2.constant.PublicConst;
import com.tuocent.dagv2.entity.Projects;
import com.tuocent.dagv2.entity.Task;
import com.tuocent.dagv2.entity.WorkReceive;
import com.tuocent.dagv2.mapper.WorkReceiveMapper;
import com.tuocent.dagv2.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工作领件 服务实现类
 * </p>
 *
 * @author Vincent
 * @since 2025-09-19
 */
@Service
public class WorkReceiveServiceImpl extends ServiceImpl<WorkReceiveMapper, WorkReceive> implements IWorkReceiveService {
    @Resource
    private RedisCache redis;
    @Autowired
    private ITaskService taskService;
    @Autowired
    private ArchivesRecordServiceImpl archivesRecordService;
    @Autowired
    private ArchivesFilesServiceImpl archivesFilesService;
    @Autowired
    private IProjectsService projectsService;

    @Override
    public AjaxResult getUserReceiveData(HttpSession session, Map<String, Object> params) {
        LoginUserInfo loginUser = redis.get(session.getId());
        int page = (int) params.getOrDefault("page", PublicConst.DEF_PAGE);
        int limit = (int) params.getOrDefault("limit", PublicConst.DEF_LIMIT);
        String pid = params.getOrDefault("pid", "").toString();
        String tid = params.getOrDefault("tid", "").toString();
        String archiveCode = params.getOrDefault("archiveCode", "").toString();
        Task task = taskService.getTask(pid, tid);
        if (task == null) {
            return AjaxResult.error("没有可获得数据的任务");
        }
        Page<?> data;
        QueryWrapper<?> qw = new QueryWrapper<>()
                .eqSql("a.id", "b.archives_id")
                .eq("b.pid", pid)
                .eq("b.tid", tid)
                .eq("b.user_id", loginUser.getUserId())
                .eq("b.status", "0");
        if (!archiveCode.isEmpty()) {
            qw.like("a.archival_code", archiveCode);
        }
        data = switch (task.getFillingModel()) {
            //按件归档
            case "1" -> archivesRecordService.getBaseMapper().getUserWorkReceive(new Page<>(page, limit), qw);
            //按卷归档
            case "2" -> archivesFilesService.getBaseMapper().getUserWorkReceive(new Page<>(page, limit), qw);
            default -> throw new IllegalStateException("Unexpected value: " + task.getFillingModel());
        };
        return AjaxResult.success(data.getRecords(), data.getTotal());
    }

}
